package top.buyibuyi.course.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.buyibuyi.course.R;
import top.buyibuyi.course.pojo.ChooseCourse;
import top.buyibuyi.course.pojo.Course;
import top.buyibuyi.course.pojo.PageBean;
import top.buyibuyi.course.service.SelectChooseService;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/course")
public class SelectChooseController {
    @Autowired
    private SelectChooseService selectChooseService;

    @PostMapping("/selectChoose/{page}/{limit}")
    public R selectChoose(@PathVariable int page, @PathVariable int limit,
                          @RequestBody ChooseCourse choose){

        PageBean<ChooseCourse> coursePageBean = new PageBean<>();
        System.out.println("页面==》"+page+"页面大小==》"+limit+"条件==》"+choose);
        //对pageBean的七个属性赋值
        //页大小
        coursePageBean.setLimit(limit);
        //总数居条数
        int count = selectChooseService.selectCount(choose);
        System.out.println("kkkkkkk"+count);
        coursePageBean.setTotal(count);
        //设置当前页
        coursePageBean.setPage(page);
        //是否有上一页
        if(page>1){
            coursePageBean.setHasPrevPage(true);
        }else {
            coursePageBean.setHasPrevPage(false);
        }
        //是否有下一页
        if(coursePageBean.getPage() >= coursePageBean.getTotalPage()){
            coursePageBean.setHasNextPage(false);
        }else {
            coursePageBean.setHasNextPage(true);
        }
        //设置数据
        try {
            int startPage = 0;
            startPage = (page-1)*limit;
            System.out.println("开始为：》》》"+startPage);
            System.out.println("...."+choose.getCname());
            System.out.println(choose.getAname());
            List<ChooseCourse> chooseCourses = selectChooseService.selectChooseCourse(startPage, limit, choose.getCname(),choose.getAname());
            coursePageBean.setPageRecode(chooseCourses);
            System.out.println(chooseCourses);
            return R.ok().data("data",coursePageBean);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("....");
        }

    }

    @GetMapping("/selectChooseById/{id}")
    public R selectChooseById(@PathVariable int id){
        System.out.println("开始看书看"+id);
        try {
            ChooseCourse chooseCourse = selectChooseService.selectChooseCourseById(id);
            System.out.println(chooseCourse);
            return R.ok().data("data",chooseCourse);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/selectByThree")
    public R selectByThree(@RequestBody ChooseCourse chooseCourse){
        System.out.println(chooseCourse);
        ChooseCourse chooseCourse1 = selectChooseService.selectByThree(chooseCourse);

        if (chooseCourse1 != null){
            return R.ok().data("data",true).message("不可以添加");
        }else {
            return R.ok().data("data",false).message("可以添加");
        }

    }
}
